//
//  InputUserIdentityController.swift
//  输入用户邮箱，手机号通用界面
//  主要用到手机号登录，找回密码时，输入手机号，输入邮箱
//  根据启动参数区分是那种功能
//  之所以设计为当前界面只是输入，下一个界面才发送验证码，原因是限制发送验证了
//  又更改了输入框的手机号，邮箱
//
//  Created by jin on 2024/9/10.
//

import UIKit

class InputUserIdentityController: BaseTitleController {
    var style: MyStyle!
    
    override func initViews() {
        super.initViews()
        
        setBackgroundColor(.colorLightWhite)
        
        initLinearLayoutInputSafeArea()
        
        container.addSubview(usernameView)
        container.addSubview(primaryButton)
    }
    
    override func initDatum() {
        super.initDatum()
        switch style {
        case .phoneLogin:
            // 手机号登录
            title = R.string.localizable.phoneLogin()
            
            usernameView.textFieldView.placeholder = R.string.localizable.enterPhone()
        default:
            // 找回密码
            title = R.string.localizable.forgotPassword()
            
            usernameView.textFieldView.placeholder = R.string.localizable.enterPhone()
        }
    }
    
    /// 文本输入框内容改变了
    @objc func textChanged(_ sender: QMUITextField) {
        primaryButton.isEnabled = SuperStringUtil.isNotBlank(sender.text?.trimmed)
    }
    
    @objc func primaryClick(_ data: QMUIButton) {
        let content = usernameView.textFieldView.text!.trimmed
        let isPhone = SuperRegularUtil.isPhone(content)
        let isEmail = SuperRegularUtil.isEmail(content)
        if isPhone || isEmail {
            let pageData = InputCodePageData()
            pageData.style = style
            if isPhone {
                pageData.phone = content
            } else {
                pageData.email = content
            }

            InputCodeController.start(navigationController!, pageData)
        } else {
            SuperToast.show(title: R.string.localizable.errorUsernameFormat())
        }
    }
    
    lazy var usernameView: SuperInputView = {
        let r = SuperInputView.input(placeholder: R.string.localizable.enterPhone())
        r.textFieldView.addTarget(self, action: #selector(textChanged(_:)), for: .editingChanged)
        
        return r
    }()
    
    lazy var primaryButton: QMUIButton = {
        let r = ViewFactoryUtil.primaryHalfFilletButton()
        r.setTitle(R.string.localizable.sendCode(), for: .normal)
        r.addTarget(self, action: #selector(primaryClick(_:)), for: .touchUpInside)
        r.isEnabled = false
        return r
    }()
}

extension InputUserIdentityController {
    static func start(_ controller: UINavigationController, _ style: MyStyle) {
        let target = InputUserIdentityController()
        target.style = style
        controller.pushViewController(target, animated: true)
    }
}
